package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.MemberService;
import com.itheima.service.OrderService;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author xyl
 * @date 2021/8/7-19:17
 */
@RestController
@RequestMapping("/charts")
public class ChartsController {

    @Reference
    private OrderService orderService;

    @Reference
    private OrderSettingService orderSettingService;

    @Reference
    private MemberService memberService;

    @RequestMapping("/getVisitAndOrderPeopleNumber")
    public Result getVisitAndOrderPeopleNumber(String date1, String date2) throws Exception {

        date1 = formatDate(date1);
        date2 = formatDate(date2);
        Map<String, Object> map = new HashMap<>();
        List<String> datelist = new ArrayList<>();      //动态时间横坐标
        List<Integer> orderlist = new ArrayList<>();    //预约人数集合
        List<Integer> visitlist = new ArrayList<>();    //就诊人数集合
        if (date1.equals(date2)){
            datelist.add(date1);
            Integer orderCount = orderSettingService.findCountByDate(date1);   //确定预约人数
            Integer visitCount = orderService.findCountByDate(date1);          //就诊人数
            System.out.println("预约人数:"+orderCount);
            System.out.println("就诊人数:"+visitCount);
            orderlist.add(orderCount);
            visitlist.add(visitCount);
            map.put("datelist",datelist);
            map.put("orderlist",orderlist);
            map.put("visitlist",visitlist);
            return new Result(true,"统计就诊、预约人数成功",map);
        }else {
            List<OrderSetting> orderSettingList = orderSettingService.findCountByDuring(date1, date2);//确定预约人数
            List<Map<String, Object>> orderToReactVisitList = orderService.findCountByDuring(date1, date2);//就诊人数
            //获取在规定时间内的 预约时间
            List<String> orderTimeList = new ArrayList<>();
            if (orderSettingList != null){
                for (int i = 0; i < orderSettingList.size(); i++) {
                    if (i==0){
                        orderTimeList.add(DateUtils.parseDate2String(orderSettingList.get(i).getOrderDate()));
                    }else if (i==orderSettingList.size()-1){
                        orderTimeList.add(DateUtils.parseDate2String(orderSettingList.get(i).getOrderDate()));
                    }else {
                        //当天预约人数
                        int todayNum = orderSettingList.get(i).getReservations();
                        //前一天预约人数
                        int yesterdayNum = orderSettingList.get(i-1).getReservations();
                        //后一天预约人数
                        int tomorrowNum = orderSettingList.get(i+1).getReservations();
                        if (todayNum-yesterdayNum != tomorrowNum-todayNum){
                            orderTimeList.add(DateUtils.parseDate2String(orderSettingList.get(i).getOrderDate()));
                        }
                    }
                }
            }
            //获取在规定时间内的 就诊时间
            List<String> visitTimeList = new ArrayList<>();
            if (orderToReactVisitList!=null){
                for (int i = 0; i < orderToReactVisitList.size(); i++) {
                    if (i == 0){
                        Date date = (Date) orderToReactVisitList.get(i).get("time");
                        visitTimeList.add(DateUtils.parseDate2String(date));
                    }else if (i == orderToReactVisitList.size()-1){
                        Date date = (Date) orderToReactVisitList.get(i).get("time");
                        visitTimeList.add(DateUtils.parseDate2String(date));
                    }else {
                        //当天的到访人数和日期
                        Long todaycount = (Long) orderToReactVisitList.get(i).get("count");
                        Date todaydate = (Date) orderToReactVisitList.get(i).get("time");
                        String todaytime = DateUtils.parseDate2String(todaydate);
                        //昨天的到访人数和日期
                        Long yesdaycount = (Long) orderToReactVisitList.get(i - 1).get("count");
                        Date yesterdaydate = (Date) orderToReactVisitList.get(i - 1).get("time");
                        String yesdaytime = DateUtils.parseDate2String(yesterdaydate);
                        //明天的到访人数和日期
                        Long tomorrowcount = (Long) orderToReactVisitList.get(i + 1).get("count");
                        Date tomorrowdate = (Date) orderToReactVisitList.get(i + 1).get("time");
                        String tomorrowtime = DateUtils.parseDate2String(tomorrowdate);
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        //前一天
                        Calendar c1 = Calendar.getInstance();
                        c1.setTime(sdf.parse(yesdaytime));
                        //今天
                        Calendar c2 = Calendar.getInstance();
                        c1.setTime(sdf.parse(todaytime));
                        //明天
                        Calendar c3 = Calendar.getInstance();
                        c1.setTime(sdf.parse(tomorrowtime));
                        //当天与前一天差值
                        int dif1 = c2.get(Calendar.DATE) - c1.get(Calendar.DATE);
                        //明天与今天差值
                        int dif2 = c3.get(Calendar.DATE) - c2.get(Calendar.DATE);
                        double k1 = (todaycount - yesdaycount) * 1.0 / dif1;
                        double k2 = (tomorrowcount - todaycount) * 1.0 / dif2;
                        if (k1 != k2) {
                            visitTimeList.add(todaytime);
                        }
                    }
                }
            }
            //取两者并集
            if (orderTimeList.size()!=0&&visitTimeList.size()!=0){
                orderTimeList.removeAll(visitTimeList);
                orderTimeList.addAll(visitTimeList);
                datelist.addAll(orderTimeList);
                datelist.sort(new Comparator<String>() {
                    @Override
                    public int compare(String o1, String o2) {
                        return o1.compareTo(o2);
                    }
                });
                for (String s : datelist) {
                    Integer oc = orderSettingService.findCountByDate(s);
                    orderlist.add(oc);
                    Integer vc = orderService.findCountByDate(s);
                    visitlist.add(vc);
                    map.put("datelist",datelist);
                    map.put("orderlist",orderlist);
                    map.put("visitlist",visitlist);
                }
                return new Result(true,"预约、到诊人数确认",map);

            } else if (orderTimeList.size()!=0&&visitTimeList.size()==0){
                datelist.addAll(orderTimeList);
                for (String s : datelist) {
                    Integer oc = orderSettingService.findCountByDate(s);
                    orderlist.add(oc);
                    map.put("datelist",datelist);
                    map.put("orderlist",orderlist);
                    map.put("visitlist",null);
                }
                return new Result(true,"预约、到诊人数确认",map);
            }else if (orderTimeList.size()==0&&visitTimeList.size()!=0){
                datelist.addAll(visitTimeList);
                for (String s : datelist) {
                    Integer vc = orderService.findCountByDate(s);
                    visitlist.add(vc);
                    map.put("datelist",datelist);
                    map.put("orderlist",null);
                    map.put("visitlist",visitlist);
                }
                return new Result(true,"预约、到诊人数确认",map);
            }else {
                return new Result(false,"选中的日期均不存在预约和访问情况");
            }
        }
    }


    @RequestMapping("/getVisitAndOrderIncome")
    public Result getVisitAndOrderIncome(String date1, String date2){

        date1 = formatDate(date1);
        date2 = formatDate(date2);
        System.out.println(date1);
        System.out.println(date2);
        List<Map<String,Object>> incomeList;
        //传来是单独一天的日期
        if (date1.equals(date2)){
            incomeList = orderSettingService.getSetmealAndIncome(date2);
            if (incomeList == null){
                return new Result(false,"该日尚无用户预约");
            }
            double income = 0;
            for (Map<String, Object> map : incomeList) {
                income += (Double) map.get("value");
            }
            Map<String,Object> m = new HashMap<>();
            m.put("name",date2+"总收入");
            m.put("value",income);
            incomeList.add(m);
            return new Result(true,"查询当日各套餐收入成功",incomeList);
        }else {     //传来的是一个时间间隔
            incomeList = orderSettingService.getDuringSetmealAndIncome(date1,date2);
            if (incomeList == null){
                return new Result(false,"该段时间尚无用户预约");
            }
            double income = 0;
            for (Map<String, Object> map : incomeList) {
                income += (Double) map.get("value");
            }
            Map<String,Object> m = new HashMap<>();
            m.put("name",date1+"~"+date2+"总收入");
            m.put("value",income);
            incomeList.add(m);
            return new Result(true,"查询当日各套餐收入成功",incomeList);
        }
    }

    @RequestMapping("/getAgeDistribution")
    public Result getAgeDistribution(){
        Map map = memberService.getAgeDistribution();
        if (map==null){
            return new Result(false,"该平台的用户尚未出示");
        }else {
            List<BigDecimal> list = new ArrayList<>();
            list.add((BigDecimal) map.get("babyCount"));
            list.add((BigDecimal) map.get("childCount"));
            list.add((BigDecimal) map.get("teenagerCount"));
            list.add((BigDecimal) map.get("youthCount"));
            list.add((BigDecimal) map.get("middleCount"));
            list.add((BigDecimal) map.get("olderCount"));
            return new Result(true,"年龄段查询成功",list);
        }
    }
    //格式化日期格式
    public String formatDate(String date){
        String[] split = date.split("-");
        String year = split[0];
        String month = split[1];
        String day = split[2];
        if (month.length()<2){
            month = "0"+month;
        }
        if (day.length()<2){
            day = "0"+day;
        }
        return year+"-"+month+"-"+day;
    }
}
